<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="appName">
              <el-input v-model="filters.appName" clearable placeholder="应用名称"></el-input>
            </el-form-item>
            <el-form-item prop="appKey">
              <el-input v-model="filters.appKey" clearable placeholder="Key"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="应用名称" prop="appName"></el-table-column>
        <el-table-column align="center" label="Key" prop="appKey"></el-table-column>
        <el-table-column align="center" label="应用类型">
          <template slot-scope="scope">
            <div>
              <span v-if="scope.row.appType === 1">呼叫器</span>
              <span v-if="scope.row.appType === 2">其它</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="最新版本号" prop="version"></el-table-column>
        <el-table-column align="center" label="版本描述" prop="describes"> </el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑 </el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpload" @click.native="handleClickUpload(scope.row)">上传新版本</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>

      <el-dialog title="上传新版本" :visible.sync="uploadDialogShow" width="30%" :before-close="uploadHandleClose">
        <div>
          <el-form :model="uploadForm" :rules="uploadRules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="版本号" prop="version">
              <el-input type="number" v-model="uploadForm.version"></el-input>
            </el-form-item>
            <el-form-item label="文件上传" prop="url">
              <upload-img class="upload-img" @uploadFileList="getFileUrl" folder="btnImg">
                <img v-if="uploadForm.url" width="100%" :src="uploadForm.url" alt="" />
                <div class="tip">
                  <i class="el-icon-upload" v-if="!uploadForm.url"></i>
                  <div v-if="!uploadForm.url" class="el-upload__text">将图片拖到此处，或<em>点击上传</em></div>
                </div>
              </upload-img>
            </el-form-item>
            <el-form-item label="描述" prop="describes">
              <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入描述" v-model="uploadForm.describes"> </el-input>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="uploadHandleClose">取 消</el-button>
          <el-button type="primary" @click="uploadHandleConfirm">确 定</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import SoftwareApi from '@/api/organ/software';
import UploadImg from '@/components/UploadImg';
export default {
  name: 'SoftwareManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      listLoading: true,
      filters: {
        appName: '',
        appKey: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      dataList: [],

      permissionCheck: 'SYSTEM_MANAGER_ORGAN_SOFTWARE_LIST_SEE',
      permissionAdd: 'SYSTEM_MANAGER_ORGAN_SOFTWARE_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_ORGAN_SOFTWARE_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_ORGAN_SOFTWARE_LIST_DELETE',
      permissionUpload: 'SYSTEM_MANAGER_ORGAN_SOFTWARE_LIST_UPLOAD',
      uploadDialogShow: false,
      uploadForm: {
        version: '',
        url: '',
        describes: '',
      },
      id: 0,
      uploadRules: {
        version: [{ required: true, message: '请输入版本号', trigger: 'blur' }],
        url: [{ required: true, message: '请上传文件', trigger: 'blur' }],
        // describes: [
        //     { required: true, message: '请输入描述', trigger: 'blur' },
        // ],
      },
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {
    UploadImg,
  },
  watch: {},
  methods: {
    init() {
      this.listLoading = true;
      this.listData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode === 13) {
        this.onSearch();
      }
    },
    getFileUrl(val) {
      if (val) {
        this.$Utils.notifySuccess('上传成功');
      }
      this.uploadForm.url = val[0];
    },
    handleClickUpload(row) {
      this.id = row.appId;
      this.uploadDialogShow = true;
    },
    uploadHandleClose() {
      this.uploadDialogShow = false;
      this.uploadForm = {
        version: '',
        url: '',
        describes: '',
      };
    },
    uploadHandleConfirm() {
      SoftwareApi.uploadVersion({
        version: this.uploadForm.version,
        url: this.uploadForm.url,
        describes: this.uploadForm.describes,
        appId: this.id,
      }).then((res) => {
        if (res.status == 0) {
          this.listData();
          this.$Utils.notifySuccess('操作成功');
          this.uploadHandleClose();
        } else {
          this.$message.error(res.msg);
        }
      });
      this.uploadDialogShow = false;
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该应用', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.appId,
          state: -1,
        };
        SoftwareApi.deleteSoftware(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/organ/softwareUpdateEdit',
        query: { id: val.appId },
      });
    },
    handleClickAdd() {
      this.$router.push('/organ/softwareUpdateAdd');
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/organ/softwareUpdateDetail',
        query: { id: row.appId },
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        appName: this.filters.appName,
        appKey: this.filters.appKey,
      };
      this.listLoading = true;
      SoftwareApi.listSoftware(cond)
        .then((res) => {
          if (res.status == 0) {
            this.dataList = res.data.list;
            this.page.pageTotal = res.data.total;
          }
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
